package com.prs.crm.action.ca.work;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.Properties;

import org.apache.struts2.convention.annotation.Action;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.domain.ca.work.ArticleTypes;
import com.prs.crm.domain.ca.work.DemandInventory;
import com.prs.crm.service.ca.work.DemandInventoryService;
/**
 * @author YangHao
 * @E-Mail:450098606@qq.com
 * @qq:450098606
 * @version 创建时间：2012-4-16 10:33:45 库存查询
 */
public class DemandInventoryAction extends BaseAction{

	private static final long serialVersionUID = -2853841250341166960L;
	private DemandInventory demandInventory;
	private PaginationSupport pagination;
	private DemandInventoryService demandInventoryService;
	private Connection con;
	private Statement stmt;
	private ResultSet rs;
	private ResultSet rs1;
	private ResultSet rs2;
	private ResultSet rs3;
	private Statement stmt1;
	private Statement stmt2;
	private Statement stmt3;
	
	public DemandInventoryService getDemandInventoryService() {
		return demandInventoryService;
	}
	public void setDemandInventoryService(
			DemandInventoryService demandInventoryService) {
		this.demandInventoryService = demandInventoryService;
	}
	public DemandInventory getDemandInventory() {
		return demandInventory;
	}
	public void setDemandInventory(DemandInventory demandInventory) {
		this.demandInventory = demandInventory;
	}
	
	public PaginationSupport getPagination() {
		return pagination;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}
	@Action("listDemandInventory")
	public String listDemandInventory() throws SQLException{
		DetachedCriteria criteria=this.getPaginationHelper().createQuery(DemandInventory.class);
		pagination=this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getDemandInventoryService().listByPage(criteria, pagination));		
		return SUCCESS;
	}
	
	@Action("selectDemandInventory")
	public String selectDemandInventory(){
		DetachedCriteria criteria=this.getPaginationHelper().createQuery(DemandInventory.class);
		pagination=this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getDemandInventoryService().listByPage(criteria, pagination));
		return SUCCESS;
	}
	@Action("viewDemandInventory")
	public String viewDemandInventory(){
		DetachedCriteria criteria=this.getPaginationHelper().createQuery(DemandInventory.class);
		pagination=this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getDemandInventoryService().listByPage(criteria, pagination));
		return SUCCESS;
	}
	
	@Action("findDemandInventory")
	public String findDemandInventory(){
		DetachedCriteria criteria=this.getPaginationHelper().createQuery(DemandInventory.class);
		pagination=this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getDemandInventoryService().listByPage(criteria, pagination));
		return SUCCESS;
	}
	@Action("editDemandInventory")
	public String edit(){
		return SUCCESS;
	}
	public void listDemand() throws SQLException{	
			List<DemandInventory> di=this.getDemandInventoryService().getAllDemandInventory();
			for (DemandInventory dis : di) {
				Properties pro=new Properties();
				String path = Thread.currentThread().getContextClassLoader().getResource("connect.properties").getPath();
				try {
					pro.load(new FileInputStream(path));
				} catch (FileNotFoundException e1) {
					e1.printStackTrace();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
				String ClassName=pro.getProperty("ClassName");
				String userName=pro.getProperty("userName");
				String userPass=pro.getProperty("userPass");
				String url1=pro.getProperty("url1");
				try {
					Class.forName(ClassName);
					con = DriverManager.getConnection(url1, userName, userPass);
					stmt = this.con.createStatement();
					stmt1 = this.con.createStatement();
					stmt2 = this.con.createStatement();
					stmt3 = this.con.createStatement();
				rs=stmt.executeQuery("select * from app_demandInventory where di_articleName='"+dis.getDi_articleName()+"'");
//				System.out.println("select * from app_demandInventory where di_articleName='"+dis.getDi_articleName()+"'");
				if (!rs.next()) {
					rs1=stmt1.executeQuery("select * from app_articleType where typename='"+dis.getDi_articletype().getCa_at_typeName()+"'");
					while (rs1.next()) {
						String ids=rs1.getString("id");
						String sql="insert into app_demandInventory(di_articleName,di_articleStandard,di_articleNumber,di_articleUnit,di_articleUnivalence,di_money,di_articletype_id)" +
								"values('"+dis.getDi_articleName()+"','"+dis.getDi_articleStandard()+"','"+dis.getDi_articleNumber()+"','"+dis.getDi_articleUnit()+"','"+dis.getDi_articleUnivalence()+"','"+dis.getDi_money()+"','"+ids+"')";
						stmt2.executeUpdate(sql);
					}
					rs1.close();	
				}
				else{
					String ids1=rs.getString("id");
					String sql1="update app_demandInventory set di_articleNumber='"+dis.getDi_articleNumber()+"', di_money='"+dis.getDi_money()+"' where id='"+ids1+"'";
					stmt3.executeUpdate(sql1);
				}
				rs.close();
				stmt.close();
				con.close();
				} catch (ClassNotFoundException e) {
					e.printStackTrace();
				}
			}
	}
}
